package com.zs.backend.config;

import cn.hutool.core.io.unit.DataSize;
import java.io.File;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UploadFileConfig {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // tmp.dir参数在启动脚本中设置
        String path = System.getProperty("user.dir");
        String location = path + "/uploadTmp";
        File tmpFile = new File(location);
        // 如果临时目录不存在则创建
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        // 明确指定上传文件的临时目录
        factory.setLocation(location);
        //文件最大
//        factory.setMaxFileSize(DataSize.parse("50MB")); //KB,MB
//        /// 设置总上传数据总大小
//        factory.setMaxRequestSize(DataSize.parse("1024MB"));
        return factory.createMultipartConfig();
    }
}
